<template>
  <div>
       <!-- 搜索栏 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="借阅者">
          <el-input v-model="formInline.username" placeholder="请输入借阅者"></el-input>
        </el-form-item>
        <el-form-item label="图书名称">
          <el-input v-model="formInline.bookName" placeholder="请输入图书名称"></el-input>
        </el-form-item>
        <el-form-item label="借阅数量">
          <el-input v-model="formInline.number" placeholder="请输入借阅数量"></el-input>
        </el-form-item>
        <!-- 开始时间 -->
        <el-form-item class="block">
          <span class="demonstration">创建时间</span>
          <el-date-picker
            v-model="formInline.begin"
            type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
    <!-- 结束时间 -->
        <el-form-item class="block">
          <span class="demonstration">创建时间</span>
          <el-date-picker
            v-model="formInline.end"
            type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getAllPageBack" class="el-icon-search">查询</el-button>
        </el-form-item>
    </el-form>
        <!-- 主体表格 -->
      <el-table :data="backList" style="width: 100%">
      <el-table-column prop="username" label="借阅者" width="120"></el-table-column>
      <el-table-column prop="bookName" label="图书名称" width="150"></el-table-column>
      <el-table-column prop="number" label="借阅数量" width="150"></el-table-column>
      <el-table-column label="借阅状态" width="150">已归还</el-table-column>
      <el-table-column prop="borrowTime" label="借阅时间" width="180"></el-table-column>
      <el-table-column prop="backTime" label="归还时间" width="150"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
        <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

        <!-- 分页条 -->
    <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 8, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
  </div>
</template>

<script>
import { getBackList, deleteById } from '@/api/back.js'
export default {
  name: 'BackPage',
  data () {
    return {
      backList: [],
      page: 1,
      pageSize: 5,
      total: null,
      formInline: {
        bookName: null,
        username: null,
        number: null,
        begin: null,
        end: null
      }
    }
  },
  created () {
    this.getAllPageBack()
  },
  methods: {
  // 删除借阅记录
    del (row) {
      this.$confirm('确定要删除该借阅记录吗？删除后将不可恢复!', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteById(row.id)
        this.getAllPageBack()
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    // 分页查询所有已归还的借阅信息
    async getAllPageBack () {
      const { data } = await getBackList(this.page, this.pageSize, this.formInline)
      this.backList = data.records
      this.total = data.total
    },
    // 每页条数改变时触发
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.getAllPageBack()
    },

    // 当前页码改变时触发
    handleCurrentChange (page) {
      this.page = page
      // 调用分页查询
      this.getAllPageBack()
    }

  }
}
</script>

<style>

</style>
